const { DataTypes, Model, Deferrable, TINYINT, Op } = require("sequelize");
const sequelize = require("../../db/sequelize");
const User = require("../user/user");
const GoodsSkus = require("../goods/goods_skus");
const { Error } = require("../../utils/newError");
const Goods = require("../goods/goods");
const { NULL } = require("mysql/lib/protocol/constants/types");
/**
 * 订单商品模型
 */
class OrderItem extends Model {
    static async getOrderItemList(page,limit,title){
        let where={}
        if (title) {
            where["title"]={}
            where.title[Op.like]="%"+title+"%"
        }
        let json={}
        json["list"]=[]
        let row=await OrderItem.findAll({
            include:[
                {
                    model:Goods,
                    as:"goods_item",
                    where:where
                },
                {
                    model:User
                }
            ],
            limit:limit,
            offset:limit*page-limit
        })

        row.forEach(value=>{
            json.list.push(value.toJSON())
        })
        json["totalCount"]=await this.count({
            include:[
                {
                    model:Goods,
                    as:"goods_item",
                    where:where
                }
            ]
        })
        return json
    }
    static async addOrderItem(order_id,goods_id,goods_skus_id,user_id,num){
        //查对应的商品规格
        let goods_skus=await GoodsSkus.findOne({
            where:{
                id:goods_skus_id,
                goods_id:goods_id
            }
        })
        if (goods_skus==null) {
            throw Error("商品规格不存在",{skus_id:goods_skus_id,goods_id:goods_id})
        }
        let price=goods_skus.pprice//获取商品价格
        //计算商品折扣
        price=price
        //减少库存
        if (goods_skus.stock>=num) {
            await goods_skus.increment({
                stock:-num
            })
        }else{
            throw Error("库存不足",{goods_id:goods_id,goods_skus_id:goods_skus_id})
        }
        //创建订单模型
        return await this.create({
            order_id:order_id,
            goods_id:goods_id,
            user_id:user_id,
            goods_skus_id:goods_skus_id,
            price:price,
            num:num,
        })
    }
    static async getReview(goods_id){
        return await this.findAll({
            where:{
                goods_id:goods_id,
                review_time:{
                    [Op.not]: NULL
                },
                review:{
                    [Op.not]:NULL
                }
            },
            include:[
                {
                    model:User,
                    attributes:["nickname","avatar"]
                },{
                    model:GoodsSkus,
                    attributes:["skus"]
                }
            ],
            attributes:["review","review_time","extra","rating"]
        })
    }
};
OrderItem.init({
    id:{
        type:DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    order_id:{
        type:DataTypes.INTEGER   
    },
    goods_skus_id:{
        type:DataTypes.INTEGER
    },
    num:{
        type:DataTypes.INTEGER
    },
    price:{
        type:DataTypes.DECIMAL(10,2),
        get(){
            return Number(this.getDataValue('price'))
        }
    },
    rating:{
        type:DataTypes.INTEGER
    },
    review:{
        type:DataTypes.TEXT
    },
    review_time:{
        type:DataTypes.DATE
    },
    goods_id:{
        type:DataTypes.INTEGER
    },
    goods_num:{
        type:DataTypes.INTEGER
    },
    user_id:{
        type:DataTypes.INTEGER
    },
    extra:{
        type:DataTypes.TEXT
    },
    status:{
        type:DataTypes.TINYINT,
        defaultValue:1
    }
}, {
    sequelize,
    modelName: "OrderItem",
    tableName: "order_item",
    createdAt:"create_time",
    updatedAt:"update_time",
})
module.exports = OrderItem